'/
' GUI Library
' Written by Anton Berlin, 2010.
' 
' =========================================================================
' 
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU Lesser General Public License as published
' by the Free Software Foundation; either version 3 of the License.
' 
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
' 
' You should have received a copy of the GNU Lesser General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'/

superstrict

import BRL.Threads
import BRL.Reflection
import BRL.PolledInput

import "../RabMT/RabMT.bmx"

type GUI_InputThread

	'// -------------------------------------------------------------------------------------------------------------
	'// Input thread.
	'// -------------------------------------------------------------------------------------------------------------
	function inputThread:object(arg:object)
	
		RabMT.newMutex("__GUI__input_thread");
		
		local inputMutex:TMutex = RabMT.getMutex("__GUI__input_thread");
		local curChar:short = 0;
		
		while (not m_endInputThread)
		
			if (not curChar) ..
				curChar = getChar();
			if (curChar) then
			
				if (inputMutex.tryLock()) then
				
					m_currentKey = curChar;
					inputMutex.unlock();
					curChar = 0;
				
				end if
			
			end if
		
		end while
		
		return null;
	
	end function
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Get the currently pressed key.
	'// -------------------------------------------------------------------------------------------------------------
	function getCurrentKey:short()
	
		local inputMutex:TMutex = RabMT.getMutex("__GUI__input_thread");
		local key:short = 0;
		
		if (inputMutex.tryLock())
		
			key = m_currentKey;
			m_lastKey = m_currentKey;
			m_currentKey = 0;
			inputMutex.unlock();
		
		end if
		
		return key;
	
	end function
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Make sure you never create an instance of this object, since it's acting as a namespace.
	'// -------------------------------------------------------------------------------------------------------------
	method new()
	
		local id:string = TTypeId.forObject(self).name()
		runtimeError("Illegal to create an instance of a namespace class '" + id + "'.")
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Current key, set by the input thread.
	'// -------------------------------------------------------------------------------------------------------------
	global m_currentKey:short = 0;
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Last pressed key.
	'// -------------------------------------------------------------------------------------------------------------
	global m_lastKey:short = 0;
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Set to true to end the input thread.
	'// -------------------------------------------------------------------------------------------------------------
	global m_endInputThread:byte = false;

end type
